# Topic: Lists, Tuples, and Functions (No Standard Deviation)

# ==========================================
#        HELPER FUNCTIONS AREA
# (You will write functions here first)
# ==========================================

def calculate_mean(data_list):
    """Returns the arithmetic mean of a list."""

    # TODO 1: (Step 1)
    # Calculate and return the average of the list.

    # WRITE YOUR CODE____start

    avg = sum(data_list)/len(data_list)
    # Remove this 'pass' and write your code
    return float(avg)
    # WRITE YOUR CODE____end


def assign_letter_grade(score):
    """
    Assigns a letter grade using parallel lists.
    """
    # 1. List of Letter Grades
    grades = ['AA', 'BA', 'BB', 'CB', 'CC', 'DC', 'DD', 'FD', 'FF']

    # 2. List of Thresholds (Limits) corresponding to grades
    thresholds = [90, 80, 70, 60, 50, 45, 35, 25, 0]

    # TODO 4: (Step 3)
    # Iterate through thresholds to find and return the correct grade.
    # If score >= threshold[i], return grades[i].
    # WRITE YOUR CODE____start
    for scr in range(0,len(thresholds)):
        if score >=thresholds[scr]:
            #print(grades[scr])
            return grades[scr]





    #for scr in student_averages:
     #   it = 0
      #  if scr >= thresholds[it]:
          #  print(grades[it])
        #    return grades[it]

       # else:
          #  it+=1



    # WRITE YOUR CODE____end


def count_grades_with_lists(student_letters, all_possible_grades):
    """
    Counts letter grades using nested loops.
    Returns a list of tuples: [('AA', 5), ('BB', 2)...]
    """
    results = []

    # TODO 6: (Step 4)
    # Use nested loops to count how many students got each grade.
    # Append the result as a tuple (grade, count) to the 'results' list.
    # WRITE YOUR CODE____start
     # I disabled this part cause it ccauses problems
    counterr = 0
    for gr in all_possible_grades:
        for ltr in student_letters:
            if ltr == gr:
                counterr += 1

                results.append((gr,counterr))
                print(results)



    # WRITE YOUR CODE____end

    return results


# ==========================================
#        MAIN EXECUTION AREA
# (The program starts running from here)
# ==========================================

# --- DATA (LISTS) ---
midterm_scores = [20, 30, 40, 50, 60, 70, 80, 90, 100]
homework_scores = [20, 30, 40, 50, 60, 70, 80, 90, 100]
project_scores = [20, 30, 40, 50, 60, 70, 80, 90, 100]
final_scores = [20, 30, 40, 50, 60, 70, 80, 90, 100]

# Reference list for counting
possible_grades = ['AA', 'BA', 'BB', 'CB', 'CC', 'DC', 'DD', 'FD', 'FF']
# Weights (Tuple): Midterm, Homework, Project, Final
weights = (0.30, 0.10, 0.20, 0.40)

print("---------------")
print("CLASS ANALYSIS SYSTEM")
print("---------------")


# ---------------------------------------------------------
# STEP 1: Calculate the mean (average) for each task
# ---------------------------------------------------------
# INSTRUCTION: Go to TOP and fill 'calculate_mean' (TODO 1).

# TODO 2: Call 'calculate_mean' for each task list.
# Store in variables: mean_midterm, mean_homework, mean_project, mean_final.
# Print the results.
# WRITE YOUR CODE____start
midterm_avg = calculate_mean(midterm_scores)
homework_avg = calculate_mean(homework_scores)
project_avg = calculate_mean(project_scores)
final_avg = calculate_mean(final_scores)
print(f"Midterm average:{midterm_avg:.2f}")
print(f"Homework average:{homework_avg:.2f}")
print(f"Project average:{project_avg:.2f}")
print(f"Final average:{final_avg:.2f}")



# WRITE YOUR CODE____end

print("---------------")


# ---------------------------------------------------------
# STEP 2: Calculate overall average for each student
# ---------------------------------------------------------
student_averages = []
num_students = len(midterm_scores)



# TODO 3: Calculate weighted average for each student.
# Formula: score * weight (Use the 'weights' tuple).
# Append result to 'student_averages'.
# WRITE YOUR CODE____start
for i in range(0,num_students):
    weight_avg = (midterm_scores[i]*0.30 + homework_scores[i]*0.10 + project_scores[i]*0.20 + final_scores[i]*0.40)
    student_averages.append(weight_avg)

#Can also be written as (midterm_scores[i]*weights[i])

##################3


# WRITE YOUR CODE____end


# ---------------------------------------------------------
# STEP 3: Assign letters and list students
# ---------------------------------------------------------
# INSTRUCTION: Go to TOP and fill 'assign_letter_grade' (TODO 4).

print("STUDENT REPORT CARD")
print(f"{'Student':<10} {'Average':<10} {'Letter Grade':<15}")

class_letter_grades = []

# TODO 5: Loop through students (num_students).
# 1. Get the average score from 'student_averages'.
# 2. Call assign_letter_grade(avg).
# 3. Append the letter to 'class_letter_grades'.
# 4. Print student info.
# WRITE YOUR CODE____start
for std in range(0, num_students):
    current = student_averages[std]
    avg = assign_letter_grade(current)
    class_letter_grades.append(avg)
    print(f"Student {std+1}   {current}          {avg}")
# WRITE YOUR CODE____end

print("---------------")

# Calculate Class Overall Mean (Optional Check)
if len(student_averages) > 0:
    overall_mean = calculate_mean(student_averages)
    print(f"CLASS OVERALL MEAN: {overall_mean:.2f}")

print("---------------")


# ---------------------------------------------------------
# STEP 4: Grade Distribution
# ---------------------------------------------------------
# INSTRUCTION: Go to TOP and fill 'count_grades_with_lists' (TODO 6).

print("GRADE DISTRIBUTION")

# TODO 7: Call the counting function and print the results.
# WRITE YOUR CODE____start
print(count_grades_with_lists(class_letter_grades,possible_grades))


# WRITE YOUR CODE____end
